<template>
  <el-dialog :title="'项目管理'" :visible.sync="show" fullscreen>
    <span v-if="value">
      <template v-if="Array.isArray(value)">
        <el-tabs v-model="tabName" type="border-card" editable @edit="tabEdit">
          <el-tab-pane v-for="(item,i) in data" :key="i" :label="data[i].name" :name="i.toString()">
            <!-- <p0 :value="item" @change="data[i]=$event; change($event)" /> -->
            <p0 :value="item" @change="data[i]=$event;" />
          </el-tab-pane>
        </el-tabs>
      </template>
      <template v-else>
        <el-form label-width="80px" style="height: 500px;overflow: hidden; " class="y jc wf">
          <div v-if="value.pid" class="ybl">
            <p1 :value="data" @change="data=$event;" />
          </div>
          <div v-else class="x jc zb">
            <el-form-item label="项目名称" style="min-width: 220px">
              <el-input placeholder="请输入项目名称" v-model="data.name" />
            </el-form-item>
            <el-form-item label="隶属部门" style="min-width: 220px">
              <sUserType v-model="data.userType" @change="data.userType=$event;" />
            </el-form-item>
          </div>
        </el-form>
      </template>
    </span>
    <span slot="footer" class="dialog-footer">
      <div class="x zb">
        <div class="x">
          <vue-xlsx-table v-if="Array.isArray(value)" @on-select-file="inFile">导入项目</vue-xlsx-table>
        </div>
        <div class="x">
          <el-button @click="show = false">取 消</el-button>
          <el-button type="primary" @click="send(data)">确 定</el-button>
        </div>
      </div>
    </span>
  </el-dialog>
</template>
<script>
import p0 from "../create/project/p0";
import p1 from "../create/project/p1";
import sUserType from "../select/sUserType";
export default {
  components: { p0, p1, sUserType },
  props: {
    value: { default: false },
    fshow: { default: false }
  },
  watch: {
    fshow(a) {
      this.show = true;
    },
    value() {
      this.first();
    }
  },
  data() {
    return {
      show: false,
      data: [],
      title: "",
      tabName: 0,
      showIn: false
    };
  },
  // computed() {return{}},
  created() {
    this.first();
  },
  methods: {
    inFile(a) {
      let data = a.body;
      let i = 0;
      let j = 8;
      let t = [
        "项目",
        "列次",
        "隶属部门",
        "物料工序",
        "物料编号",
        "物料名称",
        "物料型号",
        "预计用量"
      ];
      for (i = 0; i < a.header.length; i++) {
        if (t.indexOf(a.header[i]) >= 0) j--;
      }
      if (j) {
        this.$message.error("导入表格数据 表头必须包含 " + JSON.stringify(t));
        return;
      }
      let items = [];
      data.forEach(item => {
        i = items.getItem({ name: item.项目 }, true);
        if (i < 0) {
          i = items.length;
          items.push({ name: item.项目, info: [] });
        }
        j = items[i].info.getItem({ name: item.列次 }, true);
        if (j < 0) {
          j = items[i].info.length;
          items[i].info.push({
            name: item.列次,
            userType: item.隶属部门,
            items: []
          });
        }
        items[i].info[j].items.push({
          process: item.物料工序,
          uid: item.物料编号,
          name: item.物料名称,
          model: item.物料型号,
          sum: parseFloat(item.预计用量)
        });
      });
      console.log(this.$cp(items[0].info[0].items));
      // items.arr2tree()
      this.data = items;
    },
    send(a) {
      this.show = false;
      this.$post(
        {
          cmd: Array.isArray(a) ? 2 : 1,
          val: { token: sessionStorage.token, table: "project", info: a }
        },
        res => {
          // console.log(res);
          // return;
          if (res.result) {
            this.$formatGD(res.result);
            this.$gd.project = res.result.project;
            this.$gd.stock = res.result.stock;
            this.$gd.items = res.result.items;

            this.$message.success("数据提交成功");
            this.$emit("change");
          } else {
            this.$message.error("提交失败,请检查信息后重新提交");
            console.log(res);
          }
        }
      );
      // this.$emit("change", this.data);
    },
    first() {
      if (this.value) {
        this.data = this.$cp(this.value);
      }
    },
    proving(a, b, c) {
      this.form.info[a].items[b].sum = c;
      if (!c || !Number(c)) return;
      let t = c;
      
      this.form.info[a].items[b].sum = t;
    },
    tabEdit(a, b) {
      if (b === "add") {
        this.data.push({ name: "", info: [] });
      } else if (b === "remove") {
        this.data.splice(b, 1);
      }
    }
  }
};
</script>

<style lang="less" scoped>
</style>
